package com.yantong.controller.platform;

import com.yantong.annotation.Log;
import com.alibaba.fastjson.JSONObject;
import com.yantong.constant.BusinessCode;
import com.yantong.constant.ConfigKey;
import com.yantong.controller.BaseController;
import com.yantong.exception.BDException;
import com.yantong.pojo.Dispatch;
import com.yantong.pojo.PlatformConfig;
import com.yantong.result.ResultVo;
import com.yantong.service.ConfigService;
import com.yantong.service.OssService;
import com.yantong.utils.ValidateUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @author kanghaixin
 * @date 2018/8/26.14:54
 */
@Controller
@RequestMapping("/platform/config")
public class ConfigController extends BaseController {
    private static final String prefix = "platform";
    @Autowired
    private OssService ossService;
    @Autowired
    private ConfigService configService;


    /**
     * 基础信息配置界面
     *
     * @param model
     * @return
     */
    @GetMapping()
    public String platformConfig(Model model) {
        model.addAttribute("editable", 0);
        model.addAttribute("centreId", getUserCentreId());

        model.addAttribute("mallCodeFlag", false);
        model.addAttribute("flag", getUserFlag());

        return prefix + "/platformConfig";
    }

    /**
     * 获取基础配置信息
     *
     * @return
     */
    @ResponseBody
    @PostMapping("/base")
    public ResultVo getBaseConfig() {
        PlatformConfig config = configService.getPlatformConfigByKey(ConfigKey.BASE_CONFIG.name()
                , getUserCentreId());
        if (config == null) {
            config = new PlatformConfig();
            config.setConfigKey(ConfigKey.BASE_CONFIG.name());
        }
        return ResultVo.ok().put("baseConfig", config);
    }

    /**
     * 图片选择界面
     *
     * @param inputId
     * @param imgId
     * @param model
     * @return
     */
    @GetMapping("/selectImg/{inputId}/{imgId}")
    public String selectImg(@PathVariable String inputId, @PathVariable String imgId, Model model) {
        model.addAttribute("inputId", inputId);
        model.addAttribute("imgId", imgId);
        return prefix + "/selectImg";
    }

    /**
     * 图片批量上传
     *
     * @return
     */
    @GetMapping("/batchUploadImg/{domId}")
    public String batchUploadImg(@PathVariable String domId, Model model) {
        model.addAttribute("domId", domId);
        return prefix + "/batchUploadImg";
    }

    /**
     * 图片上传
     *
     * @param file
     * @return
     */
    @RequestMapping("/upload")
    @ResponseBody
    public ResultVo uploadImg(@RequestParam("file") MultipartFile file) {
        String result = ossService.uploadImg(file, null);
        return ResultVo.ok().put("key", result);
    }

    /**
     * 后台图片上传
     *
     * @param file
     * @return
     */
    @RequestMapping("/upload2")
    @ResponseBody
    public ResultVo uploadImg2(@RequestParam("file") MultipartFile file) {
        long size = file.getSize();
        if (file.getOriginalFilename().contains(".mp4") && size > 20 * 1048576) {
//            return ResultVo.error();
            throw  new BDException("文件过大，请选择小于20MB的文件");
        }
        if (!file.getOriginalFilename().contains(".mp4") && size > 2 * 1048576) {
            //return ResultVo.error();
            throw  new BDException("文件过大，请选择小于2MB的文件");
        }
        String result = ossService.uploadImg(file, null);
        return ResultVo.ok().put("key", result);
    }

    @RequestMapping("/upload1")
    @ResponseBody
    public ResultVo uploadImg1(@RequestParam("file") MultipartFile[] file) {
        String result = "";
        for (int i = 0; i < file.length; i++) {
            long size = file[i].getSize();
            if (size > 1048576 * 4) {
                throw  new BDException("文件过大，请选择小于4MB的文件");
                //return ResultVo.error();
            }
            result += ossService.uploadImg(file[i], null) + ",";
        }
        //System.out.println(result);
        return ResultVo.ok().put("key", result);
    }

    /**
     * 图片批量上传，
     *
     * @param model
     * @return
     * @author baolingyu
     * @date 2018年11月8日-下午4:42:32
     */
    @GetMapping("/batchUploadImgUeditor")
    public String batchUploadImgUeditor(Model model, String inputId) {
        if (ValidateUtil.isNotEmptyString(inputId)) {
            model.addAttribute("inputId", inputId);
        }
        return prefix + "/batchUploadImgUeditor";
    }

    /**
     * 选择模版
     *
     * @return java.lang.String
     * @author liaozhaogui
     * @date 2020/11/13 10:58
     */
    @GetMapping("/toInformationTemplate/{id}")
    public String platformConfig( Model model, @PathVariable("id") Long id) {
        model.addAttribute("id", id);
        return prefix + "/toInformationTemplate";
    }

    /**
     * 配置保存
     *
     * @param platformConfig
     * @return
     */
    @Log(module = "配置管理", operation = "保存配置")
    @PostMapping("/saveConfig")
    @ResponseBody
    public ResultVo saveConfig(PlatformConfig platformConfig) {
        if (platformConfig == null) {
            return ResultVo.error();
        }
        platformConfig.setOperator(getNikeName());
        platformConfig.setOperatorId(getUserId());
        if (ConfigKey.TEMPLATE_CONFIG.name().equals(platformConfig.getConfigKey())) {
            platformConfig.setCentreId(getUserCentreId());
        }

        if (ConfigKey.GZHTEMPLATE_CONFIG.name().equals(platformConfig.getConfigKey())) {
            platformConfig.setCentreId(getUserCentreId());
        }

        if (ConfigKey.FOOTER_CONFIG.name().equals(platformConfig.getConfigKey())) {
            platformConfig.setCentreId(getUserCentreId());
        }
        if (ConfigKey.TRADE_CONFIG.name().equals(platformConfig.getConfigKey())) {
            platformConfig.setCentreId(getUserCentreId());
        }
        if (ConfigKey.SHARE_CONFIG.name().equals(platformConfig.getConfigKey())) {
            platformConfig.setCentreId(getUserCentreId());
        }
        if (ConfigKey.COMMIS_CONFIG.name().equals(platformConfig.getConfigKey())) {
            platformConfig.setCentreId(getUserCentreId());
        }
        if(ConfigKey.ASSET_CONFIG.name().equals(platformConfig.getConfigKey())
                ||ConfigKey.LIFE_CONFIG.name().equals(platformConfig.getConfigKey())){
            platformConfig.setCentreId(getUserCentreId());
        }
        if(ConfigKey.CARD_CONFIG.name().equals(platformConfig.getConfigKey())){
            platformConfig.setCentreId(getUserCentreId());
        }
        if (ConfigKey.BASE_CONFIG.name().equals(platformConfig.getConfigKey())) {
            platformConfig.setCentreId(getUserCentreId());
            if (getUserCentreId() != null) {
                //平台
                PlatformConfig config = configService.getConfigByKey(ConfigKey.BASE_CONFIG.name());
                //平台配置
                Map<String, Object> configToMap = JSONObject.parseObject(config.getConfigValue());
                //站点
                Map<String, Object> configValueConfigMap = JSONObject.parseObject(platformConfig.getConfigValue());
                configValueConfigMap.put("joinTel", configToMap.get("joinTel"));
                configValueConfigMap.put("joinPic", configToMap.get("joinPic"));
                //加盟电话始终默认是平台的
                configValueConfigMap.put("mallCode", configToMap.get("mallCode"));
                platformConfig.setConfigValue(configValueConfigMap.toString());
            }
        }
        if (configService.updateConfig(platformConfig) < 1) {
            return ResultVo.error();
        }
        return ResultVo.ok();
    }


    /**
     * 获取模板配置信息
     *
     * @return
     */
    @ResponseBody
    @PostMapping("/template")
    public ResultVo getTemplateConfig() {
        PlatformConfig config = configService.getPlatformConfigByKey(ConfigKey.TEMPLATE_CONFIG.name()
                , getUserCentreId());
        if (config == null) {
            config = new PlatformConfig();
            config.setConfigKey(ConfigKey.TEMPLATE_CONFIG.name());
        }
        return ResultVo.ok().put("templateConfig", config);
    }

    /**
     * 获取公众号模板配置信息
     *
     * @return
     */
    @ResponseBody
    @PostMapping("/GZHtemplate")
    public ResultVo getGZHTemplateConfig() {
        PlatformConfig config = configService.getPlatformConfigByKey(ConfigKey.GZHTEMPLATE_CONFIG.name()
                , getUserCentreId());
        if (config == null) {
            config = new PlatformConfig();
            config.setConfigKey(ConfigKey.GZHTEMPLATE_CONFIG.name());
        }
        return ResultVo.ok().put("GZHtemplateConfig", config);
    }

    /**
     * 选择公众号模版
     *
     * @return java.lang.String
     * @author TaoHao
     * @date 2021/3/8 10:58
     */
    @GetMapping("/toInformationGZHTemplate/{id}")
    public String platformGZHConfig( Model model, @PathVariable("id") Long id) {
        model.addAttribute("id", id);
        return prefix + "/toInformationGZHTemplate";
    }

    /**
     * 本地生活配置信息
     *
     * @return
     */
    @ResponseBody
    @PostMapping("/life")
    public ResultVo getLifeConfig() {
        //PlatformConfig config = configService.getConfigByKey(ConfigKey.SHARE_CONFIG.name(),getUserCentreId());
        PlatformConfig config = configService.getPlatformConfigByKey(ConfigKey.LIFE_CONFIG.name(), getUserCentreId());
        if (config == null) {
            config = new PlatformConfig();
            config.setConfigKey(ConfigKey.LIFE_CONFIG.name());
        }
        return ResultVo.ok().put("lifeConfig", config);
    }

    /**
     * 本地生活配置信息
     *
     * @return
     */
    @ResponseBody
    @PostMapping("/card")
    public ResultVo getCardConfig() {
        PlatformConfig config = configService.getPlatformConfigByKey(ConfigKey.CARD_CONFIG.name(), getUserCentreId());
        if (config == null) {
            config = new PlatformConfig();
            config.setConfigKey(ConfigKey.CARD_CONFIG.name());
        }
        return ResultVo.ok().put("cardConfig", config);
    }


    /**
     * 资产设置
     */
    @ResponseBody
    @PostMapping("/asset")
    public ResultVo getAssetConfig() {
        //PlatformConfig config = configService.getConfigByKey(ConfigKey.TRADE_CONFIG.name(), getUserCentreId());
        PlatformConfig config =
                configService.getPlatformConfigByKey(ConfigKey.ASSET_CONFIG.name(), getUserCentreId());
        if (config == null) {
            config = new PlatformConfig();
            config.setConfigKey(ConfigKey.ASSET_CONFIG.name());
        }
        return ResultVo.ok().put("assetConfig", config);
    }

    /**
     * 佣金设置
     *
     * @return
     */
    @ResponseBody
    @PostMapping("/commis")
    public ResultVo getCommisConfig() {
        //PlatformConfig config = configService.getConfigByKey(ConfigKey.COMMIS_CONFIG.name(),getUserCentreId());
        PlatformConfig config =
                configService.getPlatformConfigByKey(ConfigKey.COMMIS_CONFIG.name(), getUserCentreId());
        if (config == null) {
            config = new PlatformConfig();
            config.setConfigKey(ConfigKey.COMMIS_CONFIG.name());
        }
        return ResultVo.ok().put("commisConfig", config);
    }

    /**
     * 关注分享配置信息
     *
     * @return
     */
    @ResponseBody
    @PostMapping("/share")
    public ResultVo getShareConfig() {
        //PlatformConfig config = configService.getConfigByKey(ConfigKey.SHARE_CONFIG.name(),getUserCentreId());
        PlatformConfig config = configService.getPlatformConfigByKey(ConfigKey.SHARE_CONFIG.name(), getUserCentreId());
        if (config == null) {
            config = new PlatformConfig();
            config.setConfigKey(ConfigKey.SHARE_CONFIG.name());
        }
        return ResultVo.ok().put("shareConfig", config);
    }



    /**
     * 获取页脚配置，区分商户
     *
     * @return
     */
    @ResponseBody
    @PostMapping("/footer")
    public ResultVo getFooterConfig() {
        //PlatformConfig config = configService.getConfigByKey(ConfigKey.FOOTER_CONFIG.name(), getUserCentreId());
        PlatformConfig config = configService.getPlatformConfigByKey(ConfigKey.FOOTER_CONFIG.name(), getUserCentreId());
        if (config == null) {
            config = new PlatformConfig();
            config.setCentreId(getUserCentreId());
            config.setConfigKey(ConfigKey.FOOTER_CONFIG.name());
        }
        return ResultVo.ok().put("footerConfig", config);
    }

    /**
     * 交易设置
     *
     * @return
     */
    @ResponseBody
    @PostMapping("/trade")
    public ResultVo getTradeConfig() {
        //PlatformConfig config = configService.getConfigByKey(ConfigKey.TRADE_CONFIG.name(), getUserCentreId());
        PlatformConfig config =
                configService.getPlatformConfigByKey(ConfigKey.TRADE_CONFIG.name(), getUserCentreId());
        if (config == null) {
            config = new PlatformConfig();
            config.setConfigKey(ConfigKey.TRADE_CONFIG.name());
        }
        return ResultVo.ok().put("tradeConfig", config);
    }


    /**
     * OSS 配置
     *
     * @return
     */
    @ResponseBody
    @PostMapping("/oss")
    public ResultVo getOssConfig() {
        PlatformConfig config = configService.getConfigByKey(ConfigKey.OSS_CONFIG.name());
        if (config == null) {
            config = new PlatformConfig();
            config.setConfigKey(ConfigKey.OSS_CONFIG.name());
        }
        return ResultVo.ok().put("ossConfig", config);
    }

}
